<?php

class Menu extends AppLog
{
	function draw($path, &$menu, &$parentMenu, &$vociMenu, &$tipoUrl, &$modMenu, &$doMenu, &$totMenu)
	{
		$vociMenu = array();
		$tmp1 = array();
		$tmp2 = array();
		$tipoUrl = array();
		$modMenu = array();
		$doMenu = array();
		$menu = array();
		$parentMenu = array();
		//$this->appLog->logWrite("Menu: $path");
		if ( ! is_dir( $path )) return;

		if ($dh = opendir($path)) 
		{
			$totMenu = 0;
			while (($file = readdir($dh)) !== false) 
			{
				if ( is_dir( "$path/$file" ) && $file != '.' && $file != '..' )
				{						
					$tmp1 = file( "$path/$file/menu.ini" );
					foreach($tmp1 as $val)
					{
						if ( preg_match( '/^disabled/' , $val ) ) break;
						
						$this->chomp($val);
						$tmp2 = preg_split(';', $val);
						
						if ( $tmp2[0] == 'order' )
						{
							$order[ $totMenu ] = intval( $tmp2[1] );
							$pathFile[ $totMenu ] = "$path/$file";
							$totMenu++;
							break;
						}
					}
				}
			}

			for( $i = 0 ; $i < $totMenu ; $i++ )
			{
				for( $j = 0 ; $j < $totMenu - 1 ; $j++ )
				{
					if ( $order[$j] > $order[$j + 1] )
					{
						$tmp = $pathFile[ $j ];
						$pathFile[ $j ] = $pathFile[ $j + 1 ];
						$pathFile[ $j + 1 ] = $tmp;
						
						$tmp = $order[ $j ];
						$order[ $j ] = $order[ $j + 1 ];
						$order[ $j + 1 ] = $tmp;
					}
				}
			}
		}
		closedir($dh);

		$totMenu = -1;
		foreach ( $pathFile as $file ) 
		{
			if ( is_dir( "$file" ) )
			{						
				$tmp1 = file( "$file/menu.ini" );
				foreach($tmp1 as $val)
				{
					$this->chomp($val);
					$tmp2 = split(";", $val);
					
					switch( $tmp2[0] )
					{
						case 'menu':
							$totMenu++;
							
							$menu[$totMenu] = $tmp2[1];
							
							$items = -1;
							$itemsId = '';
							$itemsIdPrev = '';
							$parentMenu[$totMenu][0] = '';
							$vociMenu[$totMenu][0] = array();
							$tipoUrl[$totMenu][0] = array();
							$modMenu[$totMenu][0] = array();
							$doMenu[$totMenu][0] = array();
							
							break;
					
						case 'order':
							break;
							
						case 'items':
							$itemsId = $tmp2[3];
							break;
					
						default:
							if ( $itemsId == '' )
								$items++;
							else if ( $itemsIdPrev != $itemsId ) 
								$items++;
							
							if ( $itemsId == '' )
								$parentMenu[$totMenu][ $items ] = '';
							else if ( $itemsIdPrev != $itemsId ) 
								$parentMenu[$totMenu][ $items ] = $itemsId;
							
							$tipoUrl[$totMenu][ $items ][] = $tmp2[0];
							$modMenu[$totMenu][ $items ][] = $tmp2[1];
							$doMenu[$totMenu][ $items ][] = $tmp2[2];
							$vociMenu[$totMenu][ $items ][] = $tmp2[3];

							$itemsIdPrev = $itemsId;
					}
				}
			}
			//echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
		}
	}
	
	function swapArray(&$ary1, &$ary2)
	{
		$tmp = $ary1;
		$ary1 = $ary2;
		$ary2 = $tmp;
	}
	
	function chomp(&$string) 
	{ 
		$endchar = substr("$string", strlen("$string") - 1, 1); 
		if ($endchar == "\n") 
		{ 
				$string = substr("$string", 0, -1); 
		} 
	} 
}

?>